<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/8/29
 * Time: 10:36
 */

namespace App\CronTask;

use App\Enums\UrlEnums;
use App\Library\Auth;
use App\Utility\Pool\MysqlObject;
use App\Utility\Pool\MysqlPool;
use EasySwoole\EasySwoole\Crontab\AbstractCronTask;
use EasySwoole\HttpClient\HttpClient;
use EasySwoole\MysqliPool\Mysql;

/**
 * 更新联赛
 * Class LeagueCron
 * @package App\CronTask
 */
class LeagueCron extends AbstractCronTask
{
    public static function getRule(): string
    {
        // TODO: Implement getRule() method.
        return '*/1 * * * *';
        //return '@daily';
    }

    public static function getTaskName(): string
    {
        // TODO: Implement getTaskName() method.
        // 定时任务名称
        return 'taskOne';
    }

    static function run(\swoole_server $server, int $taskId, int $fromWorkerId,$flags=null)
    {
        // TODO: Implement run() method.
        // 定时任务处理逻辑
        $tables = ['siam_kogleague','siam_csgoleague','siam_lolleague','siam_dotaleague'];
        $path = [UrlEnums::KOG_LEAGUE_LIST_PATH,UrlEnums::CSGO_LEAGUE_LIST_PATH,UrlEnums::LOL_LEAGUE_LIST_PATH,UrlEnums::DOTA_LEAGUE_LIST_PATH];
        for ( $i=0; $i< count($path) ; $i++ ){
            $leagueUrl = UrlEnums::FJURL.$path[$i].'?version=2&offset=0&limit=1000';
            $client = new HttpClient($leagueUrl);
            list($s1, $s2) = explode(' ', microtime());
            $microtime = (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000); //毫秒时间戳
            $option = [
                'path'      => $path[$i],
                'api_time'  => $microtime
            ];
            $header = Auth::getInstance()->header($option);
            $client->setHeaders($header,true,false);
            $response = $client->get();
            $data = $response->getBody();
            $data = json_decode($data,true);
            $table = $tables[$i];
            MysqlPool::invoke(function (MysqlObject $db)use($data,$table){
                if( $data['code'] == 200 && !empty($data['data']) ){
                    foreach ( $data['data'] as $item ){
                        $league = $db->where('league_id',$item['league_id'])->getOne($table);
                        if( $league ){
                            if( $league['status'] !== 2  ){
                                $res = $db->where('id',$league['id'])->update($table,['status'=>$item['status']]);
                            }
                        }else{
                            $item['team_ids'] = json_encode($item['team_ids']);
                            if( isset($item['team_prize']) ){
                                $item['team_prize'] = json_encode($item['team_prize']);
                            }
                            if( isset($item['prizes']) ){
                                $item['prizes'] = json_encode($item['prizes']);
                            }
                            if( isset($item['map_pool']) ){
                                $item['map_pool'] = json_encode($item['map_pool']);
                            }
                            $res = $db->insert($table,$item);
                        }
                    }
                }
            });
        }

    }
}